var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);

document.getElementById( 'Exploration_Race'        ).value = prefService.getIntPref( 'extensions.Ultima.YourThroneRace'        );
document.getElementById( 'Exploration_Personality' ).value = prefService.getIntPref( 'extensions.Ultima.YourThronePersonality' );
document.getElementById( 'Exploration_Relations'   ).value = prefService.getIntPref( 'extensions.Ultima.YourThroneWar'         );
document.getElementById( 'Exploration_Land'        ).value = prefService.getIntPref( 'extensions.Ultima.YourThroneLand'        );

function addCommas(argNum, argThouSeparator, argDecimalPoint) {
var sThou = (argThouSeparator) ? argThouSeparator : ","
var sDec = (argDecimalPoint) ? argDecimalPoint : "."
var aParts = argNum.split(sDec)
var sInt = aParts[0] + sDec
var rTest = new RegExp("(\\d)(\\d{3}(\\" + sThou + "|\\" + sDec + "))")
while (sInt.match(rTest)) { sInt = sInt.replace(rTest, "$1" + sThou + "$2") }
aParts[0] = sInt.replace(sDec, "")
return aParts.join(sDec)
}

function Add_Commas(Number) { return addCommas( Number.toString() ); }

function Task_Exploration() {

var Exploration_Race           = Number( document.getElementById( 'Exploration_Race'           ).value );
var Exploration_Personality    = Number( document.getElementById( 'Exploration_Personality'    ).value );
var Exploration_Stance         = Number( document.getElementById( 'Exploration_Stance'         ).value );
var Exploration_Relations      = Number( document.getElementById( 'Exploration_Relations'      ).value );
var Exploration_Land           = Number( document.getElementById( 'Exploration_Land'           ).value );
var Exploration_Mills          = Number( document.getElementById( 'Exploration_Mills'          ).value );
var Exploration_Land_Explored  = Number( document.getElementById( 'Exploration_Land_Explored'  ).value );
var Exploration_Lost_In_Combat = Number( document.getElementById( 'Exploration_Lost_In_Combat' ).value );
var Exploration_BE             = Number( document.getElementById( 'Exploration_BE'             ).value );

if ( Exploration_Land           < 0 ) document.getElementById('Exploration_Land'           ).value = 0 ;
if ( Exploration_Land_Explored  < 0 ) document.getElementById('Exploration_Land_Explored'  ).value = 0 ;
if ( Exploration_Lost_In_Combat < 0 ) document.getElementById('Exploration_Lost_In_Combat' ).value = 0 ;

if ( Exploration_Mills < 0  ) document.getElementById('Exploration_Mills' ).value = 0  ;
if ( Exploration_Mills > 50 ) document.getElementById('Exploration_Mills' ).value = 50 ;

if ( Exploration_BE < 0   ) document.getElementById( 'Exploration_BE' ).value = 0   ;
if ( Exploration_BE > 200 ) document.getElementById( 'Exploration_BE' ).value = 200 ;

Exploration_Land           = Number( document.getElementById( 'Exploration_Land'           ).value );
Exploration_Mills          = Number( document.getElementById( 'Exploration_Mills'          ).value );
Exploration_Land_Explored  = Number( document.getElementById( 'Exploration_Land_Explored'  ).value );
Exploration_Lost_In_Combat = Number( document.getElementById( 'Exploration_Lost_In_Combat' ).value );
Exploration_BE             = Number( document.getElementById( 'Exploration_BE'             ).value );

Exploration_BE = Exploration_BE / 100 ;

var Exploration_Effect        = 0 ;
var Exploration_Full_Matrix   = 0 ;
var Exploration_Half_Matrix   = 0 ;
var Exploration_Matrix        = 0 ;

var Exploration_Race_Effect        = 1 ;
var Exploration_Personality_Effect = 1 ;
var Exploration_Stance_Effect      = 1 ;
var Exploration_Mills_Effect       = 1 ;

//if ( Exploration_Race        == 5 ) Exploration_Race_Effect        = 1.2 ;
//if ( Exploration_Personality == 5 ) Exploration_Personality_Effect = 0.7 ;
if ( Exploration_Stance      == 2 ) Exploration_Stance_Effect        = 2   ; // Fortified = +100% Exploration Costs Penaltie
if ( Exploration_Relations   == 1 ) Exploration_Stance_Effect        = 2   ; // War       = +100% Exploration Costs Penaltie

Exploration_Effect        = ( Exploration_Land_Explored - Exploration_Lost_In_Combat ) / 2 ;
if ( Exploration_Effect < 0 ) Exploration_Effect = 0 ;
Exploration_Full_Matrix   = Exploration_Land - 300 ;
if ( Exploration_Full_Matrix   < 0 ) Exploration_Full_Matrix   = 0 ;
Exploration_Half_Matrix   = Math.min( Exploration_Full_Matrix , Exploration_Effect );
Exploration_Matrix        = Math.pow( 2 , ( Exploration_Full_Matrix + Exploration_Half_Matrix ) * 3 / 2500 );
Exploration_Mills_Effect  = 3 * Exploration_Mills / 100 * (1 - Exploration_Mills / 100 ) * 100 * Exploration_BE ;

if ( Exploration_Mills_Effect > 75 ) Exploration_Mills_Effect = 75 ;

Exploration_Mills_Effect = 1 - Exploration_Mills_Effect / 100 ;

Exploration_Soldiers_Cost = Math.round( ( 3 + 1/210 * Exploration_Matrix ) * Exploration_Race_Effect * Exploration_Stance_Effect * Exploration_Personality_Effect ) ;
Exploration_Gold_Cost     = Math.round( ( 600 + 2.3 * Exploration_Matrix ) * Exploration_Race_Effect * Exploration_Stance_Effect * Exploration_Personality_Effect * Exploration_Mills_Effect ) ;

document.getElementById( 'Exploration_Acres'    ).innerHTML = Add_Commas( Exploration_Land * 2                         ) ;
document.getElementById( 'Exploration_Gold'     ).innerHTML = Add_Commas( Exploration_Land * Exploration_Gold_Cost     ) ;
document.getElementById( 'Exploration_Soldiers' ).innerHTML = Add_Commas( Exploration_Land * Exploration_Soldiers_Cost ) ;

for (i=1; i<=10; i++) {

if ( Exploration_Soldiers_Cost > 99999999 ) Exploration_Soldiers_Cost = "Crazy!";
if ( Exploration_Gold_Cost     > 99999999 ) Exploration_Gold_Cost     = "Crazy!";

document.getElementById( 'Exploration_Main_td'+i+'1' ).innerHTML = i ;
document.getElementById( 'Exploration_Main_td'+i+'2' ).innerHTML = Add_Commas( Exploration_Land          ) ;
document.getElementById( 'Exploration_Main_td'+i+'3' ).innerHTML = Add_Commas( Exploration_Soldiers_Cost ) ;
document.getElementById( 'Exploration_Main_td'+i+'4' ).innerHTML = Add_Commas( Exploration_Gold_Cost     ) ;
document.getElementById( 'Exploration_Main_td'+i+'5' ).innerHTML = Math.round( Exploration_Mills_Effect * 10000 ) / 100 +'%' ;
document.getElementById( 'Exploration_Main_td'+i+'6' ).innerHTML = Add_Commas( Exploration_Land_Explored ) ;

Exploration_Land_Explored = Exploration_Land_Explored + Exploration_Land ;
Exploration_Land = Exploration_Land * 2 ;
Exploration_Mills = Exploration_Mills / 2 ;

Exploration_Effect        = ( Exploration_Land_Explored - Exploration_Lost_In_Combat ) / 2 ;
if ( Exploration_Effect < 0 ) Exploration_Effect = 0 ;

Exploration_Full_Matrix   = Exploration_Land - 300 ;
if ( Exploration_Full_Matrix   < 0 ) Exploration_Full_Matrix   = 0 ;
Exploration_Half_Matrix   = Math.min( Exploration_Full_Matrix , Exploration_Effect );
Exploration_Matrix        = Math.pow( 2 , ( Exploration_Full_Matrix + Exploration_Half_Matrix ) * 3 / 2500 );
Exploration_Mills_Effect  = 3 * Exploration_Mills / 100 * (1 - Exploration_Mills / 100 ) * 100 * Exploration_BE ;

if ( Exploration_Mills_Effect > 75 ) Exploration_Mills_Effect = 75 ;

Exploration_Mills_Effect = 1 - Exploration_Mills_Effect / 100 ;

Exploration_Soldiers_Cost = Math.round( ( 3 + 1/210 * Exploration_Matrix ) * Exploration_Race_Effect * Exploration_Stance_Effect * Exploration_Personality_Effect ) ;
Exploration_Gold_Cost     = Math.round( ( 600 + 2.3 * Exploration_Matrix ) * Exploration_Race_Effect * Exploration_Stance_Effect * Exploration_Personality_Effect * Exploration_Mills_Effect ) ;

}

}

function Task_Manager() {
Task_Exploration();
setTimeout("Task_Manager()",1);
}

Task_Manager();